package com.concurrent.chapter6.concurrent01;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class UseThreadPool {
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		final List<Integer> list = new LinkedList<>();
		int corePoolSize = 1;
		int maximumPoolSize = 1;
		long keepAliveTime = 60;
		TimeUnit unit = TimeUnit.SECONDS;
		LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>();
		ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
		final Random random = new Random();
		int count = 10000;
		//使用线程池
		for (int i = 0; i < count; i++) {
			executor.execute(new Runnable() {
				
				@Override
				public void run() {
					list.add(random.nextInt());
					System.out.println(Thread.currentThread().getName());
				}
			});			
		}
		executor.shutdown();
		try {
			executor.awaitTermination(1, TimeUnit.DAYS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(System.currentTimeMillis() - startTime);
		System.out.println(list.size());
	}	
}
